package com.ying.controller.order;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ying.core.page.PageWrap;
import com.ying.core.response.ApiResponse;
import com.ying.po.order.Order;
import com.ying.service.order.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/api/order/order")
@Api(tags = "Order接口")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/{id}")
    @SaCheckPermission("order-get")
    @ApiOperation("根据ID查询order")
    public ApiResponse finById(@PathVariable(value = "id") Integer id) {
        return ApiResponse.success(orderService.findById(id));
    }

    @PostMapping("/create")
    @SaCheckPermission("order-post")
    @ApiOperation("创建order对象")
    public ApiResponse create(@RequestBody Order order) {
        // 存储order
        Integer id = orderService.create(order);
        return ApiResponse.success(id);
    }

    @PostMapping("/page")
    @SaCheckPermission("order-get")
    @ApiOperation("分页查询")
    public ApiResponse findPage(@RequestBody PageWrap<Order> pageWrap) {
        return ApiResponse.success(orderService.findAllByPage(pageWrap));
    }

    @PutMapping("/update")
    @ApiOperation("修改Order")
    @SaCheckPermission("order-put")
    public ApiResponse updateById(@RequestBody Order order) {
        orderService.updateById(order);
        return ApiResponse.success(null);
    }

    @DeleteMapping("/delete/{id}")
    @SaCheckPermission("order-delete")
    @ApiOperation("根据ID删除")
    public ApiResponse delete(@PathVariable Integer id) {
        orderService.deleteById(id);
        return ApiResponse.success(null);
    }

    @DeleteMapping("/delete")
    @ApiOperation("根据ID列表删除")
    @SaCheckPermission("order-delete")
    public ApiResponse deleteByIds(@RequestBody List<Integer> ids) {
        orderService.deleteByIdInBatch(ids);
        return ApiResponse.success(null);
    }

    @GetMapping("/count/all")
    @ApiOperation("查询全部/某年订单数量")
    public ApiResponse countAll(@RequestParam("userId") Integer userId, @RequestParam(value = "year", required = false) Integer year) {
        long count = orderService.countAll(userId, year);
        return ApiResponse.success(count);
    }

//    @PostMapping("/findbyids")
//    @ApiOperation("根据ID列表查询")
//    public ApiResponse findByIds(@RequestBody Integer[] ids) {
//        List<Order> userList = orderService.findByIdInBatch(Arrays.asList(ids));
//        return ApiResponse.success(userList);
//    }

    @GetMapping("/find/all")
    @ApiOperation("不分页查询全部")
    @SaCheckPermission("order-get")
    public ApiResponse findAll(@RequestParam("userId") Integer userId) {
        ApiResponse success = ApiResponse.success(orderService.findAll(userId));
        return success;
    }

    @PutMapping("/status")
    @ApiOperation("修改order状态")
    @SaCheckPermission("order-put")
    public ApiResponse changeOrderStatus(@RequestParam("status") Integer status, @RequestParam("orderId") Integer orderId) {
        orderService.changeOrderStatus(status, orderId);
        ApiResponse success = ApiResponse.success();
        return success;
    }

    //   搜索框搜索
    @PostMapping("/query")
    @ApiOperation("根据条件查询Order")
    @SaCheckPermission("order-get")
    public ApiResponse queryAllByPage(@RequestBody PageWrap<Order> pageWrap) {
        return ApiResponse.success(orderService.queryOrderByCondition(pageWrap));
    }

//    getPriceList
    @GetMapping("/price/all")
    @ApiOperation("根据条件查询Order")
    @SaCheckPermission("order-get")
    public ApiResponse getPriceList(@RequestParam("userId") Integer userId) {
        return ApiResponse.success(orderService.getPriceList(userId));
    }

    @GetMapping("/weight/number")
    @ApiOperation("查询销售总重")
    @SaCheckPermission("order-get")
    public ApiResponse getNumberOfWeight(@RequestParam("userId") Integer userId, @RequestParam(value = "year", required = false) Integer year) {
        return ApiResponse.success(orderService.getNumberOfWeight(userId, year));
    }

    @GetMapping("/container/number")
    @ApiOperation("查询销售总箱数")
    @SaCheckPermission("order-get")
    public ApiResponse getNumberOfContainer(@RequestParam("userId") Integer userId,  @RequestParam(value = "year", required = false) Integer year) {
        return ApiResponse.success(orderService.getNumberOfContainer(userId, year));
    }

    @GetMapping("/money/number")
    @ApiOperation("查询销售总额")
    @SaCheckPermission("order-get")
    public ApiResponse getNumberOfMoney(@RequestParam("userId") Integer userId, @RequestParam(value = "year", required = false) Integer year) {
        return ApiResponse.success(orderService.getNumberOfMoney(userId,year));
    }

    @GetMapping("/degree/completion")
    @ApiOperation("计算订单完成度")
    @SaCheckPermission("order-get")
    public ApiResponse getOrderCompletion(@RequestParam("userId") Integer userId, @RequestParam(value = "year", required = false) Integer year) {
        return ApiResponse.success(orderService.getOrderCompletion(userId, year));
    }

    @GetMapping("/degree/rebuy")
    @ApiOperation("计算回购率")
    @SaCheckPermission("order-get")
    public ApiResponse getOrderReBuyDegree(@RequestParam("userId") Integer userId, @RequestParam(value = "year", required = false) Integer year) {
        if (year == null) {
            return ApiResponse.success(orderService.getOrderReBuyDegree(userId));
        } else {
            return ApiResponse.success(orderService.getOrderRebuyDegreeOfThisYear(userId, year));
        }
    }

    @GetMapping("/degree/express")
    @ApiOperation("计算各个快递合作率")
    @SaCheckPermission("order-get")
    public ApiResponse getExpressDegree(@RequestParam("userId") Integer userId, @RequestParam(value = "year", required = false)Integer year) {
        return ApiResponse.success(orderService.getExpressDegree(userId, year));
    }

    @GetMapping("/count/buyer/new-old")
    @ApiOperation("查询本年度新旧买家数量")
    @SaCheckPermission("order-get")
    public ApiResponse getNewAndOldBuyerCountThisYear(@RequestParam("userId")Integer userId, @RequestParam("year")Integer year) {
        return ApiResponse.success(orderService.getNewAndOldBuyerCountThisYear(userId, year));
    }

//
    @GetMapping("/statistic/chart")
    @ApiOperation("获取统计图表数据")
    @SaCheckPermission("order-get")
    public ApiResponse getChartData(@RequestParam("userId")Integer userId, @RequestParam("year")Integer year) {
        return ApiResponse.success(orderService.getChartData(userId, year));
    }

}
